package com.imooc.service;

import com.imooc.domain.Girl;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.BizException;
import com.imooc.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * Created by 廖师兄
 * 2016-11-04 00:08
 *
 * @author songjun
 */
@Service
public class GirlService {

    @Autowired
    private GirlRepository girlRepository;

    @Transactional(rollbackFor = Exception.class)
    public void insertTwo() {
        Girl girlA = new Girl();
        girlA.setCupSize("A");
        girlA.setAge(18);
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setCupSize("BBBB");
        girlB.setAge(19);
        girlRepository.save(girlB);
    }


    public Girl getAgeMsg(Integer id) {
        Optional<Girl> optional = girlRepository.findById(id);
        if (optional.isPresent()) {
            Girl girl = optional.get();
            if (girl.getAge() < 12) {
                throw new BizException(ResultEnum.PRIMARY_SCHOOL_EXCEPTION);

            } else if (girl.getAge() < 16) {
                throw new BizException(ResultEnum.HIGH_SCHOOL_EXCEPTION);

            } else if (girl.getAge() < 19) {
                throw new BizException(ResultEnum.SUPER_SCHOOL_EXCEPTION);

            }
            return girl;
        }
        return null;

    }
}
